package com.yixun.controller;

import com.yixun.entity.DiQu;
import com.yixun.service.DiQuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/diqu")
public class DiQuController extends BaseController {

    @Autowired
    private DiQuService diQuService;

    @RequestMapping(value = "/list")
    public String list(){
        return "org/diqu/diquList";
    }

    /**
     * 获取地区树
     * @param dq_id
     * @return
     */
    @RequestMapping(value = "/getDiquTree/{dq_id}")
    @ResponseBody
    public String getDiquTree(@PathVariable(value = "dq_id") Long dq_id){
        return diQuService.getDiquTree(dq_id);
    }

    /**
     * 得到下级树,不含本级,不控制级别,一般用于最后一级树
     * @param dq_id
     * @return
     */
    @RequestMapping(value = "/getAllChildDiquTree/{dq_id}")
    @ResponseBody
    public String getAllChildDiquTree(@PathVariable(value = "dq_id") Long dq_id){
        return diQuService.getAllChildDiquTree(dq_id);
    }

    /**
     * 根据地区id获取其子地区
     * @param dq_id
     * @return
     */
    @RequestMapping(value = "/getChildDiquList/{dq_id}")
    @ResponseBody
    public List<DiQu> getChildDiquList(@PathVariable(value = "dq_id") Long dq_id){
        return diQuService.getChildDiquList(dq_id);
    }

    @RequestMapping(value = "/go_diqu_add")
    public String go_diqu_add(Long parentID, Long dq_id, Model model){
        model.addAttribute("parentID", parentID);
        model.addAttribute("dq_id", dq_id);
        return "org/diqu/diqu_add";
    }

    /**
     * 判断地区是否存在
     * @param dq_id
     * @return
     */
    @RequestMapping(value = "/diquIDIsExist/{dq_id}")
    @ResponseBody
    public boolean diquIDIsExist(@PathVariable(value = "dq_id") Long dq_id){
        DiQu diQu = diQuService.getDiquByDqId(dq_id);
        return !ObjectUtils.isEmpty(diQu);
    }

    /**
     * 添加地区
     * @param diQu
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public boolean add(@RequestBody DiQu diQu){
        int row = diQuService.insertDiQu(this.getSessionUser(), diQu);
        return row == 1;
    }

    /**
     * 根据地区id获取地区
     * @param dq_id
     * @return
     */
    @RequestMapping(value = "/getDiquBean/{dq_id}")
    @ResponseBody
    public DiQu getDiquBean(@PathVariable(value = "dq_id") Long dq_id){
        return diQuService.getDiquBean(dq_id);
    }

    /**
     * 修改地区
     * @param diQu
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public boolean edit(@RequestBody DiQu diQu){
        int row = diQuService.updateDiQu(this.getSessionUser(), diQu);
        return row == 1;
    }

    /**
     * 删除地区及子地区
     * @param id
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public boolean del(String id){
        int row = diQuService.delDiQu(this.getSessionUser(), id);
        return row > 0;
    }

    @RequestMapping(value = "/go_select_single_diqu")
    public String go_select_single_diqu(String handle_name, Model model){
        model.addAttribute("handle_name", handle_name);
        return "org/diqu/select_single_diqu";
    }

    /**
     * 移动地区及其子地区
     * @param map
     * @return
     */
    @RequestMapping(value = "/move", method = RequestMethod.POST)
    @ResponseBody
    public boolean move(@RequestBody Map map){
        int row = diQuService.moveDiQu(this.getSessionUser(), map);
        return row > 0;
    }

    /**
     * 地区排序
     * @param
     * @return
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST)
    @ResponseBody
    public boolean sort(String id){
        int row = diQuService.sortDiQu(this.getSessionUser(), id);
        return row > 0;
    }
}
